package com.balabala.gym_goods.controller.admin;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.goods.constant.KafkaConstant;
import com.balabala.gym_goods.pojo.dto.AddCourseScheduleDto;
import com.balabala.gym_goods.pojo.dto.CourseByNameQueryDto;
import com.balabala.gym_goods.pojo.dto.CourseDto;
import com.balabala.gym_goods.pojo.dto.CourseQueryDto;
import com.balabala.gym_goods.pojo.entity.Course;
import com.balabala.gym_goods.pojo.vo.CourseFindResultVo;
import com.balabala.gym_goods.pojo.vo.CourseListVo;
import com.balabala.gym_goods.pojo.vo.CourseVo;
import com.balabala.gym_goods.service.CourseService;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fsj
 * @date 2023/5/5 19:15
 */
@RestController
@Slf4j
@CrossOrigin
@Api(tags = "团课管理后台接口")
@RequestMapping("/admin/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @PostMapping("/add")
    @ApiOperation("添加课程信息")

    public Result add(@RequestBody CourseDto courseDto) {
        log.info("添加一条课程信息");
        int i = courseService.addCourse(courseDto);
        if (i > 0) {
            return Result.ok();
        }
        return Result.fail();
    }

//    @PostMapping("/getCoursePageList")
//    @ApiOperation("根据课程名称查询")
//    public Result getCoursePageList(@RequestBody CourseQueryDto courseQueryDto)
//    {
//        log.info("查询课程信息，条件是：{}",courseQueryDto.toString());
//        PageInfo<CourseVo> courseVoPageInfo=courseService.getCoursePageList(courseQueryDto);
//        return Result.ok(courseVoPageInfo.getList(),courseVoPageInfo.getTotal());
//    }

    @DeleteMapping("/deleteById")
    @ApiOperation("根据id删除课程信息")
    public Result deleteById(@RequestParam Long id) {
        log.info("根据id删除课程信息");
        //发送删除该课程所支持会员卡的缓存的消息

      kafkaTemplate.send(KafkaConstant.COURSE_CARD_CLEAR,String.valueOf(id)).addCallback(success->log.info("消息发送成功：{}",success),fail-> log.info("发送消息失败",fail.getMessage()));

        log.info("kafka发送清空课程支持会员卡缓存消息完毕。。。");
        return Result.ok(ResultEnum.GENERALLY_SUCCESS, courseService.removeById(id));
    }


    @PostMapping("/update")
    @ApiOperation("修改课程信息")
    public Result update(@RequestBody Course course) {
        log.info("修改课程信息：{}", course.toString());
        return Result.judge(courseService.updateById(course));
    }


    @PostMapping("/findCourseByName")
    @ApiOperation("根据课程名称模糊查询课程信息")
    public Result findCourseByName(@RequestBody CourseByNameQueryDto courseByNameQueryDto) {
        log.info("根据课程名称模糊查询课程信息：{}", courseByNameQueryDto.toString());
        PageHelper.startPage(courseByNameQueryDto.getPage(), courseByNameQueryDto.getLimit());
        List<CourseVo> courseVoList = courseService.findCourseByName(courseByNameQueryDto);
        PageInfo<CourseVo> pageInfo = new PageInfo<>(courseVoList);
        return Result.ok(courseVoList, pageInfo.getTotal());
    }


    @GetMapping("/getCourseById")
    @ApiOperation("根据id查询课程所有信息")
    public Result findAllById(@RequestParam Long id) {
        log.info("通过查询课程信息");
        Course byId = courseService.getById(id);
        return Result.ok(ResultEnum.GENERALLY_SUCCESS, byId);
    }


    @GetMapping("/findListAll")
    @ApiOperation("查询所有团课课表列表")
    public Result findListAll() {
        return Result.ok(courseService.getListAll());
    }

    @PostMapping("/updateList")
    @ApiOperation("修改团课课表列表")
    public Result updateList(@RequestBody CourseDto courseDto) {
        Boolean aBoolean = courseService.updateList(courseDto);
        if (aBoolean) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有团课课程列表")
    public Result findAll() {
        return Result.ok(courseService.getAll());
    }


    @GetMapping("/findById/{id}")
    @ApiOperation("根据id查询课表信息")
    public Result findById(@PathVariable("id") Long id) {
        List<CourseFindResultVo> byId = courseService.findById(id);

        return Result.ok(byId);
    }

    @PostMapping("/addCourseSchedule")
    @ApiOperation("添加课表信息")
    public Result addCourseSchedule(@RequestBody  AddCourseScheduleDto addCourseScheduleDto) {

        Boolean aBoolean = courseService.addCourseSchedule(addCourseScheduleDto);
        if (aBoolean){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/deleteCourseSchedule/{id}")
    @ApiOperation("根据id删除课表信息")
    public Result deleteCourseSchedule(@PathVariable("id") Long id) {
        Boolean aBoolean = courseService.deleteByCourseScheduleId(id);
        if (aBoolean) {
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/findByCoachId/{id}")
    @ApiOperation("根据教练id查询课程信息")
    public Result findByCoachId(@PathVariable("id") Long id) {
        List<Course> courses = courseService.findByCoachId(id);
        return Result.ok(courses);
    }


}
